﻿using System.Globalization;
using UnityEngine;

public class LoadBoardMgr : Singleton<LoadBoardMgr>
{
    private HotUpdatePanel view;

    //加载的内容描述
    private string des;
    //加载的百分比
    private string per;
    //加载的进度
    private float prog;

    //替换字符串
    public string strLoad;
    public string strProg;
    public string strVers;
    public string strInfo;

    public LoadBoardMgr()
    {
        Transform trans = AllUpdate.GetChildByName("HotUpdate");
        view = new HotUpdatePanel(trans.gameObject);
        ShowLoadBoard(false); SaveLocalStr(); EventListener();
    }

    private void SaveLocalStr()
    {
        strLoad = view.Load_txt.text;
        strProg = view.Prog_txt.text;
        strVers = view.Versions_txt.text;
        strInfo = view.Info_txt.text;
    }

    private void EventListener()
    {
        UIEventListener.Get(view.Dialog_obj).onClick = OnRefuseUpdate;
        UIEventListener.Get(view.Dialog1_obj).onClick = OnAgreeUpdate;
        UIEventListener.Get(view.Dialog2_obj).onClick = OnRefuseUpdate;
    }
    
    public void ShowLoadBoard(bool bShow)
    {
        Utility.SetActive(view.Go, bShow);
    }

    public void Close()
    {
        ShowLoadBoard(false);
        Reset();
    }
    
    public void Show()
    {
        ShowLoadBoard(true);
    }
    
    public void Reset()
    {
        des = "";
        per = "0 %";
    }

    /// <summary>
    /// 询问是否更新
    /// </summary>
    public void AskUpdate()
    {
        Utility.SetActive(view.Dialog_sp, true);
        Utility.SetActive(view.Ask_obj, true);
        Utility.SetActive(view.Prog_obj, false);
        Utility.SetActive(view.Link_obj, false);

        float totalSize = ABCheckUpdate.GetSingleton().totalBytes / GlobalNum.BYTE_SIZE;
        view.Info_txt.text = string.Format(strInfo, totalSize);
    }

    /// <summary>
    /// 显示更新进度
    /// </summary>
    public void DisUpdate()
    {
        Utility.SetActive(view.Dialog_sp, false);
        Utility.SetActive(view.Ask_obj, false);
        Utility.SetActive(view.Link_obj, false);
        Utility.SetActive(view.Prog_obj, true);
        
        string clientVer = ABCheckUpdate.GetSingleton().clientVer;
        string serverVer = ABCheckUpdate.GetSingleton().serverVer;
        view.Versions_txt.text = string.Format(strVers, clientVer, serverVer);
    }

    /// <summary>
    /// 显示断开连接
    /// </summary>
    public void DisNoLink()
    {
        Utility.SetActive(view.Dialog_sp, true);
        Utility.SetActive(view.Link_obj, true);
        Utility.SetActive(view.Ask_obj, false);
        Utility.SetActive(view.Prog_obj, false);
    }

    /// <summary>
    /// 显示更新界面
    /// </summary>
    public void ShowUpdate()
    {
        Utility.SetActive(view.Tip_txt, false);
        Utility.SetActive(view.Update_obj, true);
    }

    /// <summary>
    /// 隐藏更新界面
    /// </summary>
    public void HideUpdate()
    {
        Utility.SetActive(view.Tip_txt, true);
        Utility.SetActive(view.Update_obj, false);
    }

    /// <summary>
    /// 玩家同意更新
    /// </summary>
    public void OnAgreeUpdate(GameObject go)
    {
        ZEventCenter.DispatchEvent(new ZEvent(ConstData.AGREE_UPDATE));
    }

    /// <summary>
    /// 询问是否更新
    /// </summary>
    private void OnRefuseUpdate(GameObject go)
    {
        Application.Quit();
    }

    /// <summary>
    /// 加载进度刷新
    /// </summary>
    /// <param name="value">进度比例</param>
    public void SetPercent(float value)
    {
        per = value.ToString("#0%", CultureInfo.InvariantCulture);
        prog = value; UpdateDP();
    }

    /// <summary>
    /// 加载描述
    /// </summary>
    /// <param name="e"></param>
    public void SetDescri(string value)
    {
        des = value;
        UpdateDP();
    }

    private void UpdateDP()
    {
        if (string.IsNullOrEmpty(des))
        {
            des = GlobalString.loadAss;
        }
        view.Tip_txt.text = des + per;

        view.Load_sdr.value = ABCheckUpdate.GetSingleton().LoadProgress;
        float curSize = ABCheckUpdate.GetSingleton().curBytes / GlobalNum.BYTE_SIZE;
        float totalSize = ABCheckUpdate.GetSingleton().totalBytes / GlobalNum.BYTE_SIZE;
        view.Load_txt.text = string.Format(strLoad, curSize, totalSize);
        view.Prog_txt.text = string.Format(strProg, per);
    }
}